<?php
/**
 * Description: 微信广告投放数据追踪
 * User: devforma
 * Date: 16/7/11
 * Time: 15:57
 */

class WeixinIdfa implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;

        if (empty($queryArgs['muid']) || empty($queryArgs['click_time']) || empty($queryArgs['click_id'])
            || empty($queryArgs['appid']) || empty($queryArgs['advertiser_id']) || empty($queryArgs['app_type'])) {
            return json_encode(['ret' => -1, 'msg' => 'missiong arguments']);
        }

        $exists = $uddb->getVar('SELECT `id` FROM `youqian_idfa_data` WHERE `app_id`=:appid AND `idfa`=:muid',
            [':appid' => $queryArgs['appid'], ':muid' => $queryArgs['muid']]);

        if (empty($exists)) {
            $callback = "http://t.gdt.qq.com/conv/app/{$queryArgs['appid']}/conv?v={data}&conv_type=MOBILEAPP_ACTIVITE&app_type=IOS&advertiser_id={$queryArgs['advertiser_id']}";

            $uddb->insert('youqian_idfa_data',
                [
                    'app_id' => $queryArgs['appid'],
                    'click_id' => $queryArgs['click_id'],
                    'advertiser_id' => $queryArgs['advertiser_id'],
                    'channel_id' => 'weixin',
                    'idfa' => $queryArgs['muid'],
                    'mission_status' => 0,
                    'click_time' => date('Y-m-d H:i:s', $queryArgs['click_time']),
                    'callback' => $callback
                ]
            );
        }

        return json_encode(['ret' => 0, 'msg' => 'success']);
    }

}